package com.commentlib.utils;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;

public class DateUtils {

    public static String YY_MM_DDHH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String YYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YY_MM_DD = "yyyy-MM-dd";
    public static String MM_DD = "MM-dd";
    public static String YYMMDD1 = "yyyy/MM/dd";
    public static String YYMMDD = "yyyyMMdd";
    public static String YY_MM = "yyyy-MM";
    public static String YY_MM_DDHH_MM = "yyyy-MM-dd HH:mm";
    public static String MM_DDHH_MM = "MM-dd HH:mm";
    public static String HH_MM = "HH:mm";
    public static String HH_MM_SS = "HH:mm:ss";

    /***获取当前时间戳*/
    public static long getDate() {
        Date date = new Date(System.currentTimeMillis());
        return date.getTime();
    }

    /***
     * 获取当前时间
     * @param format 时间格式
     * */
    @SuppressLint("SimpleDateFormat")
    public static String getDateTime(String format) {
        if ("".equals(format) || null == format) return "";
        SimpleDateFormat df = new SimpleDateFormat(format);//设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    /**
     * 时间字符串转date日期
     * @param dateStr 时间
     * @param format 格式
     * @return
     */
    public static Date formatDate(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        // 对于已经设定为GMT时间标准的dateFormat来说，一切需要他转换的字符串日期都是GMT标准时间，转换后返回的Date由于默认遵守系统默认时区
        //，所以转换给Date的日期需要+8（例如北京标准时区），也就是时区与标准不同导致的时差。
        Date result = null;
        try {
            result = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据格式转换时间
     * @param dateStr 转换的时间
     * @param oldFormat 当前格式
     * @param newFormat 需要转换的格式
     * @return
     */
    public static String formatDateToStr(String dateStr, String oldFormat, String newFormat) {
        SimpleDateFormat sf1 = new SimpleDateFormat(oldFormat);
        SimpleDateFormat sf2 = new SimpleDateFormat(newFormat);
        String result = dateStr;
        try {
            result = sf2.format(sf1.parse(dateStr));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据时间获取时间戳
     *
     * @param dateStr 时间
     * @param format 格式
     * @return
     */
    public static long getDateToLong(String dateStr, String format) {
        if ("".equals(dateStr) || null == dateStr) return 0;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date result = null;
        try {
            result = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result.getTime();
    }

    /**
     * 时间戳转秒
     *
     * @param date
     * @return
     */
    public static Integer transForMilliSecond(Date date) {
        if (date == null) return null;
        return (int) (date.getTime() / 1000);
    }

    /**
     * 将时间戳转换为时间
     * @param s 时间戳字符串
     * @param format 转换格式
     */
    public static String stampToDate(String s, String format) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将Date转换为时间
     * @param date 日期
     * @param format 转换格式
     */
    public static String stampDateToStr(Date date, String format) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 获取前某个月日期
     *
     * @return
     */
    public static String getMonthStr(int month) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YY_MM);
        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);
        int monthStr = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, monthStr - month);
        String months = simpleDateFormat.format(calendar.getTime());
        return months;
    }

    /**
     * 获取前一天
     *
     * @param curMonth
     * @return
     */
    public static String getLastDay(String curMonth) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YY_MM_DD);
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = simpleDateFormat.parse(curMonth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, day - 1);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取后一天
     *
     * @param curMonth
     * @return
     */
    public static String getNextDay(String curMonth, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = simpleDateFormat.parse(curMonth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, day + 1);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取某个日期的某一天
     *
     * @param curMonth
     * @return
     */
    public static String getByDay(String curMonth, int byDay, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = simpleDateFormat.parse(curMonth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, day + byDay);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取前一个月
     *
     * @param curMonth
     * @return
     */
    public static String getLastMonth(String curMonth, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = simpleDateFormat.parse(curMonth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取后一个月
     *
     * @param curMonth
     * @return
     */
    public static String getNextMonth(String curMonth, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = simpleDateFormat.parse(curMonth);
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if (month == 0 && day > 29) {
            int dayTime = 29;
            if (year % 4 == 0) {
                dayTime = 30;
            }
            calendar.set(year, month + 1, dayTime);
        } else {
            calendar.set(Calendar.MONTH, month + 1);
        }
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取day时间
     *
     * @param day
     * @return
     */
    public static String getTimeByPreOrNextDayNum(int day) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YY_MM_DDHH_MM_SS);
        Calendar calendar = Calendar.getInstance();
        Date date = null;
        try {
            date = simpleDateFormat.parse(getDateTime(YY_MM_DDHH_MM_SS));
        } catch (Exception e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int day_s = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, day_s + day);
        return simpleDateFormat.format(calendar.getTime());
    }

    /**
     * 获取星期几
     *
     * @return
     */
    public static String getWeekOfDate(String dateStr, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        int dayOfWeek = 0;
        try {
            Calendar calendar = Calendar.getInstance();
            Date date = simpleDateFormat.parse(dateStr);
            calendar.setTime(date);
            dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (dayOfWeek < 0) dayOfWeek = 0;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekDays[dayOfWeek];
    }

    /**
     *  * 获取指定日期下个月的第一天
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static String getFirstDayOfMonth(String dateStr, String format, int amount) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, amount);
            calendar.set(Calendar.DAY_OF_MONTH, 1);//第一天
//            calendar.set(Calendar.DAY_OF_MONTH, 0);//最后一天
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  * 获取当前月的日期
     * month  1 最后一天   0 第一天
     * day  1 第一天   0 最后一天
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static String getFirstOrLastDayOfMonth(String dateStr, String format, int month, int day) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            Date date = sdf.parse(dateStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);//第一天
//            calendar.set(Calendar.DAY_OF_MONTH, 0);//最后一天
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /****
     * 获取当前周的日期
     * @return
     */
    public static List<String> getWeekDay(String format) {
        Calendar calendar = Calendar.getInstance();
        // 获取本周的第一天
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek + i);
            list.add(new SimpleDateFormat(format).format(calendar.getTime()));
        }
        return list;
    }

    /****
     * 获取当前周的日期的第一天
     * @return
     */
    public static String getWeekOfOneDay() {
        Calendar calendar = Calendar.getInstance();
        // 获取本周的第一天
        int firstDayOfWeek = calendar.getFirstDayOfWeek();
        List<String> list = new ArrayList<>();
        calendar.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);
        return new SimpleDateFormat(YY_MM_DD).format(calendar.getTime());
    }

    /**
     * 获取两个时间差
     *
     * @param oneTimeStr 大时间
     * @param twoTimeStr 小时间
     * @param formatType
     * @return
     */
    public static long getTimeDifference(String oneTimeStr, String twoTimeStr, String formatType) {
        long timeDifference = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatType);
            Date oneTime = format.parse(oneTimeStr);
            Date twoTime = format.parse(twoTimeStr);
            timeDifference = oneTime.getTime() - twoTime.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Math.abs(timeDifference);
    }

    /**
     * 获取两个时间差
     *
     * @param oneTime 大时间
     * @param twoTime 小时间
     * @return
     */
    public static int getTimeDifferenceByDate(Date oneTime, Date twoTime) {
        int timeDifference = oneTime.getYear() - twoTime.getYear();
        return Math.abs(timeDifference);
    }
}
